<?php
/**
 * 轻云网络验证系统
 * =======================================================
 * 版权所有(C) 2019-2039 daenx.cn，并保留所有权利。
 * 作者：Daen大恩
 * 作者QQ: 1330166565
 * 开发群：661059773
 * =======================================================
 */
error_reporting(0);//屏蔽错误,防止暴路径
header('Content-Type:application/json; charset=utf-8');
include '../data/config.php';
include('../data/sdk/class.rc4crypt.php');//引入SDK文件,方便快捷！


$key = "dnqyyz"; //KEY必须与易语言同步
$rc4 = new Crypt_RC4();//初始化SDK
$rc4 -> setKey($key);//设置RC4密匙

$sign = $_GET['sign'];
$op = $_GET['op'];
$cp = $_GET['cp'];
$km = $_GET['km'];
$dl = $_GET['dl'];
$name = $_GET['name'];
$ip = $_GET['ip'];
$other = $_GET['other'];
$name_new = $_GET['name_new'];
$user = $_GET['user'];
$newuser = $_GET['newuser'];
$user_new = $_GET['user_new'];
$pass = $_GET['pass'];
$pass_new = $_GET['pass_new'];
$key_cp = $_GET['key_cp'];
$key_sys = $_GET['key_sys'];
$key_user = $_GET['key_user'];
$free = 0;
$toemail = $_GET['toemail'];
$etitle = $_GET['title'];
$econtent = $_GET['content'];
$qq = $_GET['qq'];
$num = $_GET['num'];
$mod = $_GET['mod'];
$txt = $_GET['txt'];
$sign = $_GET['sign'];
$timegg = $_GET['time'];
$nowtime=time();

$title = "";
$yxtime = "";
$copyright = "";
$site_name = "";
$route = "";
$robotqq = "";
$apikey = "";
$smtpserver = "";
$smtpserverport = "";
$smtpusermail = "";
$smtpuser = "";
$smtppass = "";
$kf = 0;
$qd = 0;
$qdrmb = 0;
$kmqz = "";
$kmcd = 0;

get_sys();
$jm_key = md5($apikey);
$sign_data = $_SERVER["QUERY_STRING"];//获取网址中的全部参数
parse_str($sign_data,$sign_array);//将全部参数转化为数组格式

verifySign($jm_key,$sign_array);

function verifySign($secret, $data) {
    // 验证参数中是否有签名
    if (!isset($data['sign']) || !$data['sign']) {
		$data1 = array('status'=>'0','message'=>'发送的数据签名不存在');
        $ec = json_encode($data1);
		echo $ec;
        die();
    }
    if (!isset($data['timestamp']) || !$data['timestamp']) {
		$data1 = array('status'=>'0','message'=>'发送的数据参数不合法');
        $ec = json_encode($data1);
		echo $ec;
        die();
    }
    // 验证请求， 10分钟失效
    //if (time()-60*60*8+96 - $data['timestamp'] > 600) {  //部分服务器时间不准确的话，可以用此方法来校准时间
	if (time() - $data['timestamp'] > 600) {	
		$data1 = array('status'=>'0','message'=>'验证失效，请重新发送请求'.time().'');
        $ec = json_encode($data1);
		echo $ec;
        die();
    }
    $sign = $data['sign'];
    unset($data['sign']);
	unset($data['timestamp']);
    ksort($data);
    $params = http_build_query($data);
    $sign2 = md5($params . $secret);
    if ($sign == $sign2) {
        
    } else {
		$data1 = array('status'=>'0','message'=>'请求不合法');
        $ec = json_encode($data1);
		echo $ec;
        die();
    }
}

//————————————————————————————测试接口
if($op=='ip'){//测试

	echo getip();
	//echo $op;
}

//—————————————————————————————————————私有接口
if($op=='mail_send'){//发送邮件
	require_once "../data/Smtp.class.php";
    $mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件
    $smtp = new Smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
    $smtp->debug = false;//是否显示发送的调试信息
    $state = $smtp->sendmail($toemail, $smtpusermail, $etitle, $econtent, $mailtype);
	if($state!="")
	{$data = array('status'=>'1','message'=>'发送成功');}
	else
	{$data = array('status'=>'0','message'=>'发送失败');}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='api_test'){//接口key测试
	$ip = getip();
	$data = array('status'=>'1','message'=>'签名验证成功','ip'=>$ip);
	$ec = json_encode($data);
	echo $ec;
}

if($op=='ins_sq'){//添加用户
	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else{
	if($pass==null)
	{$data = array('status'=>'0','message'=>'密码获取错误');}
	else{
	if($qq==null)
	{$data = array('status'=>'0','message'=>'qq获取错误');}
	else{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else{
			
		$sql="select * from authorize where domain='$user' and ycp='$cp'";
		$query=mysqli_query($con,$sql);
		$rows = mysqli_num_rows($query);
		if($rows > 0)
		{$data = array('status'=>'0','message'=>'该账号已存在');}
		else
		{
			if($name == null){$name = $user;}
			$km = getkm(12);
			
			$sql = "INSERT INTO `authorize` (`username`,`domain`,`pass`,  `qq`,  `time`, `syskey`, `ycp`, `dl`) VALUES ('$name','$user','$pass','$qq','$timegg','$km','$cp','普通用户');";//添加授权
			$query=mysqli_query($con,$sql);
			$data = array('status'=>'1','message'=>'添加成功','key'=>$km);}	
		
	}}}}}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='del_sq'){//删除用户
	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else{
		$sql="select * from authorize where domain='$user' and ycp='$cp'";
		$query=mysqli_query($con,$sql);
		$row = mysqli_fetch_array($query);
		$rows = mysqli_num_rows($query);
		$id = $row[id];
		if($rows == 0)
		{$data = array('status'=>'0','message'=>'账号不存在');}
		else
		{
			$sql="DELETE FROM `authorize` WHERE `id`='$id'";
			mysqli_query($con,$sql);
			$data = array('status'=>'1','message'=>'删除成功');
		}		
	}}}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='get_allcp'){//获取产品

	$sql="select * from products ORDER BY `id` DESC";
	$result=mysqli_query($con,$sql);
	if($result&&mysqli_num_rows($result))
		{
			$dataarr = array(); 
			class products//定义一个类
			{
				public $cp;
				public $qt;
				public $notice;
				public $newver;
				public $qz;
				public $key_cp;
				public $free;
				public $zt;
				public $url;
				public $jg1;
				public $jg2;
				public $jg3;
				public $jg4;
				public $jg5;
				public $jg6;
			}
			while ($row=mysqli_fetch_assoc($result))
			{
				
				$products = new products();
				$products->cp = $row["cp"];
				$products->qt = $row["qt"];
				$products->notice = $row["ms"];
				$products->newver = $row["nver"];
				$products->qz = $row["qz"];
				$products->key_cp = $row["nkey"];
				$products->free = $row["free"];
				$products->zt = $row["zt"];
				$products->url = $row["url"];
				
				$products->jg1 = $row["jg1"];
				$products->jg2 = $row["jg2"];
				$products->jg3 = $row["jg3"];
				$products->jg4 = $row["jg4"];
				$products->jg5 = $row["jg5"];
				$products->jg6 = $row["jg6"];
				$data[] = $products;
				
			}
			$ec = json_products('1','获取成功',$data); 
		}
		else
		$ec = json_products('0','获取失败'); 
	
	echo $ec;
}

if($op=='ins_daoban'){//上报盗版

		if($user==null){$user='无';}
		if($key_cp==null)
		{$data = array('status'=>'0','message'=>'产品key获取错误');}
		else
		{
			$cp = get_cp($key_cp);
			if($cp=="error")
			{$data = array('status'=>'0','message'=>'产品key错误');}
			else
			{
				$ip=getip();
				$ver=get_ver($key_cp);
				$sql = "INSERT INTO `daoban` (`domain`,`time`,`ver`,`ip`,`ycp`,`qq`, `beizhu`) VALUES ('$user','$nowtime','$ver','$ip','$cp','$qq','$other');";
				$query=mysqli_query($con,$sql);
				$data = array('status'=>'1','message'=>'上报成功');	
			}
		}
	
	$ec = json_encode($data);
	echo $ec;
}

if($op=='ins_fk'){//提交反馈

	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	{
	if($txt==null)
	{$data = array('status'=>'0','message'=>'内容获取错误');}
	else
	{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	{
		
	$SQL = "SELECT * FROM `authorize` WHERE `domain`='$user' and `ycp`='$cp'";
	$query=mysqli_query($con,$SQL);
	while($row =mysqli_fetch_array($query))
	{
		$breath = $row[yumi];//用户状态
		$domain = $row[domain];
		$dpass = $row[pass];
		$rmb = $row[rmb];
		$id = $row[id];
		$bz = $row[bz];
		if($breath==1){$breath='0';}else{$breath='1';}//用户状态取反											
	}
	if($user != $domain)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'无授权');}
	else
	{
	$time = time();
	$ip = getip();
	$sql = "INSERT INTO `log_fk` (`domain`,`time`,`cp`,`bz`,`ip`) VALUES ('$user', '$time', '$cp', '$txt','$ip');";//添加反馈日志
	mysqli_query($con,$sql);
	$data = array('status'=>'1','message'=>'提交反馈成功');
	}}}}}
	$ec = json_encode($data);
	echo $ec;
}
if($op=='ins_rmb_dl'){//充值余额_代理

	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	{
	if($num==null)
	{$data = array('status'=>'0','message'=>'金额获取错误');}
	else
	{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	{
	$SQL = "SELECT * FROM `daili` WHERE `domain`='$user' and `ycp`='$cp'";
	$query=mysqli_query($con,$SQL);
	while($row =mysqli_fetch_array($query))
	{
		$breath = $row[yumi];//用户状态
		$domain = $row[domain];
		$rmb = $row[rmb];
		$id = $row[id];										
	}
	if($user != $domain)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'非代理');}
	else
	{
	$zrmb = $rmb + $num;
	$dtime = time();
	$qrmb = '+'.$num;
	$sql = "INSERT INTO `log_dl` (`uid`,`time`,`cp`,`rmb`,`zrmb`,`bz`,`ip`) VALUES ('$id', '$dtime', '$cp', '$qrmb', '$zrmb', '管理员充值_api','');";//添加流水日志
	mysqli_query($con,$sql);
	$sql = "UPDATE `daili` SET `rmb` = '$zrmb' WHERE `id` = '$id';";
	mysqli_query($con,$sql);
	$data = array('status'=>'1','cp'=>$cp,'user'=>$user,'rmb'=>$num,'zrmb'=>"$zrmb");
	}}}}}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='ded_rmb_dl'){//扣除余额_代理

	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	{
	if($num==null)
	{$data = array('status'=>'0','message'=>'金额获取错误');}
	else
	{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	{
	$SQL = "SELECT * FROM `daili` WHERE `domain`='$user' and `ycp`='$cp'";
	$query=mysqli_query($con,$SQL);
	while($row =mysqli_fetch_array($query))
	{
		$breath = $row[yumi];//用户状态
		$domain = $row[domain];
		$rmb = $row[rmb];
		$id = $row[id];										
	}
	if($user != $domain)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'非代理');}
	else
	{
	$zrmb = $rmb - $num;
	$dtime = time();
	$qrmb = '-'.$num;
	$sql = "INSERT INTO `log_dl` (`uid`,`time`,`cp`,`rmb`,`zrmb`,`bz`,`ip`) VALUES ('$id', '$dtime', '$cp', '$qrmb', '$zrmb', '管理员扣除_api','');";//添加流水日志
	mysqli_query($con,$sql);
	$sql = "UPDATE `daili` SET `rmb` = '$zrmb' WHERE `id` = '$id';";
	mysqli_query($con,$sql);
	$data = array('status'=>'1','cp'=>$cp,'user'=>$user,'rmb'=>$num,'zrmb'=>"$zrmb");
	}}}}}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='ins_rmb'){//充值余额

	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	{
	if($num==null)
	{$data = array('status'=>'0','message'=>'金额获取错误');}
	else
	{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	{
	
	$SQL = "SELECT * FROM `authorize` WHERE `domain`='$user' and `ycp`='$cp'";
	$query=mysqli_query($con,$SQL);
	while($row =mysqli_fetch_array($query))
	{
		$breath = $row[yumi];//用户状态
		$domain = $row[domain];
		$rmb = $row[rmb];
		$id = $row[id];
		if($breath==1){$breath='0';}else{$breath='1';}//用户状态取反											
	}
	if($user != $domain)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'无授权');}
	else
	{
	if($breath == 0)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'禁用中');}
	else
	{	

	$zrmb = $rmb + $num;
	$dtime = time();
	$qrmb = '+'.$num;
	ins_log($user,$dtime,$cp,$qrmb,$zrmb,'API增加');
	$sql = "UPDATE `authorize` SET `rmb` = '$zrmb' WHERE `id` = '$id';";
	mysqli_query($con,$sql);
	$data = array('status'=>'1','cp'=>$cp,'user'=>$user,'rmb'=>$num,'zrmb'=>$zrmb);
	}}}}}}
	$ec = json_encode($data);
	echo $ec;
}
 
if($op=='ded_rmb'){//扣除余额

	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	{
	if($num==null)
	{$data = array('status'=>'0','message'=>'金额获取错误');}
	else
	{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	{
	
	$SQL = "SELECT * FROM `authorize` WHERE `domain`='$user' and `ycp`='$cp'";
	$query=mysqli_query($con,$SQL);
	while($row =mysqli_fetch_array($query))
	{
		$breath = $row[yumi];//用户状态
		$domain = $row[domain];
		$rmb = $row[rmb];
		$id = $row[id];
		if($breath==1){$breath='0';}else{$breath='1';}//用户状态取反											
	}
	if($user != $domain)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'无授权');}
	else
	{
	if($breath == 0)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'禁用中');}
	else
	{	
	if($num > $rmb)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'余额不足');}	
	else
	{
	$zrmb = $rmb - $num;
	$dtime = time();
	$qrmb = '-'.$num;

	ins_log($user,$dtime,$cp,$qrmb,$zrmb,'API扣除');
	$sql = "UPDATE `authorize` SET `rmb` = '$zrmb' WHERE `id` = '$id';";
	mysqli_query($con,$sql);
	$data = array('status'=>'1','cp'=>$cp,'user'=>$user,'rmb'=>$num,'zrmb'=>$zrmb);
	}}}}}}}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='ins_time'){//充值时间

	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	{
	$SQL = "SELECT * FROM `authorize` WHERE `domain`='$user' and `ycp`='$cp'";
	$query=mysqli_query($con,$SQL);
	while($row=mysqli_fetch_array($query))
	{
		$breath = $row[yumi];//用户状态
		$domain = $row[domain];
		$dqtime = $row[time];
		$id = $row[id];
		
		if($breath==1){$breath='0';}else{$breath='1';}//用户状态取反											
	}
	if($user != $domain)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'无授权');}
	else
	{
	if($breath == 0)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'禁用中');}
	else
	{	

		if($dqtime=="free")
		{
			if($timegg == '')
			{$time = '';}
			else
			{$time = time() + $timegg;}
		}
		
		else
		{
			if($dqtime==""){
			$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'已是永久');
			}else{
				if($timegg == '')
				{$time = '';}
				else 
				{
					if(time() > $dqtime)
					{
					$time = time() + $timegg;
					}else
					{$time = $dqtime + $timegg;}
				}
				$sql = "UPDATE `authorize` SET `time` = '$time' WHERE domain='$user' and ycp='$cp';";//更新到期时间
				mysqli_query($con,$sql); 
				if($time == ''){
					$dsc = '永久';
				}else{
					$dsc = date("Y-m-d H:i:s",$time);
				}
				$data = array('status'=>'1','cp'=>$cp,'user'=>$user,'time'=>$dsc);	
				
			}	
		}

	}}}}}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='ded_time'){//扣除时间

	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	{
	$SQL = "SELECT * FROM `authorize` WHERE `domain`='$user' and `ycp`='$cp'";
	$query=mysqli_query($con,$SQL);
	while($row=mysqli_fetch_array($query))
	{
		$breath = $row[yumi];//用户状态
		$domain = $row[domain];
		$dqtime = $row[time];
		$id = $row[id];
		
		if($breath==1){$breath='0';}else{$breath='1';}//用户状态取反											
	}
	if($user != $domain)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'无授权');}
	else
	{
	if($breath == 0)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'禁用中');}
	else
	{	

		if($dqtime=="free")
		{
			$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'免费用户，无法扣除时间');
		}
		
		else
		{
			if($dqtime==""){
			$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'永久授权，无法扣除时间');
			}else{
				if($timegg == '')
				{$time = 'free';}
				else 
				{
				$time = $dqtime - $timegg;
				}
				$sql = "UPDATE `authorize` SET `time` = '$time' WHERE domain='$user' and ycp='$cp';";//更新到期时间
				mysqli_query($con,$sql); 
				if($time == 'free'){
					$dsc = '免费用户';
				}else{
					$dsc = date("Y-m-d H:i:s",$time);
				}
				$data = array('status'=>'1','cp'=>$cp,'user'=>$user,'time'=>$dsc);	
				
			}	
		}

	}}}}}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='ins_km'){//生成卡密

	if($num==null)
	{$data = array('status'=>'0','message'=>'金额获取错误');}
	else
	{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	{
		$km=getkm();
		$time=time();
		$sql =  "INSERT INTO `cami_ye` (`km`, `time`, `ye`, `cp`,`up`) VALUES ('$km', '$time', '$num', '$cp','system');";//生成卡密
		mysqli_query($con,$sql);
		$data = array('status'=>'1','cp'=>$cp,'km'=>$km,'rmb'=>$num);
	}}}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='make_user'){//操作用户

	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	{
	if($mod==null)
	{$data = array('status'=>'0','message'=>'指令获取错误');}
	else
	{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	{
	$SQL = "SELECT * FROM `authorize` WHERE `domain`='$user' and `ycp`='$cp'";
	$query=mysqli_query($con,$SQL);
	while($row =mysqli_fetch_array($query))
	{
		$breath = $row[yumi];//用户状态
		$domain = $row[domain];
		$rmb = $row[rmb];
		$id = $row[id];
		if($breath==1){$breath='0';}else{$breath='1';}//用户状态取反											
	}
	if($user != $domain)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'无授权');}
	else
	{
	
		if($mod == 1)
		{
			if($mod==1){$mod=0;}else{$mod=1;}	
			if($breath == 1)
			{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'已经是正常状态');}
			else
			{	
			$sql = "UPDATE `authorize` SET `yumi` = '$mod' WHERE `id` = '$id';";
			mysqli_query($con,$sql);
			$data = array('status'=>'1','cp'=>$cp,'user'=>$user,'breath'=>'1');
			}
		}
		else
		if($mod == 0)
		{
			if($mod==1){$mod=0;}else{$mod=1;}	
			if($breath == 0)
			{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'已经是禁用状态');}
			else
			{	
			$sql = "UPDATE `authorize` SET `yumi` = '$mod' WHERE `id` = '$id';";
			mysqli_query($con,$sql);
			$data = array('status'=>'1','cp'=>$cp,'user'=>$user,'breath'=>'0');
			}
		}
		
	}
	
	}}}}
	$ec = json_encode($data);
	echo $ec;
}
//—————————————————————————————————————公有接口

if($op=='ins_user'){//用户注册
	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	if($pass==null)
	{$data = array('status'=>'0','message'=>'密码获取错误');}
	else
	if($qq==null)
	{$data = array('status'=>'0','message'=>'qq获取错误');}
	else
	{
		$ycp = get_cp($key_cp);	
		if($ycp == 'error')
			{$data = array('status'=>'0','message'=>'产品key错误');}
			else
			{
		$sql="select * from products where cp='$ycp'";
		$result=mysqli_query($con,$sql);
		if($result&&mysqli_num_rows($result)){
		while ($row=mysqli_fetch_assoc($result)) {
			$kf = $row['kf'];}}
		if($kf != 1)
		{$data = array('status'=>'0','message'=>'未开放注册');}
		else
		{	
				$sql="select * from authorize where domain='$user' and ycp='$ycp'";
				$query=mysqli_query($con,$sql);
				$rows = mysqli_num_rows($query);
				if($rows > 0)
				{$data = array('status'=>'0','message'=>'该账号已存在');}
				else
				{
					require_once "../data/Smtp.class.php";
					$mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件
					$smtp = new Smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
					$smtp->debug = false;//是否显示发送的调试信息
					if($name == null){$name = $user;}
					$ucl = '*ycp='.$ycp.'*username='.$name.'*domain='.$user.'*pass='.$pass.'*qq='.$qq;
					$econtent = '请点击下方链接完成注册验证<br>http://'.$_SERVER['HTTP_HOST'].'/self-zc.php?submit=1'.base64_encode($rc4->encrypt($ucl));
					$toemail = $qq.'@qq.com';
					$etitle = $title.'-注册验证';
					$state = $smtp->sendmail($toemail, $smtpusermail, $etitle, $econtent, $mailtype);

					if($state!="")
					{$data = array('status'=>'1','message'=>'注册链接已经已发送到您的QQ邮箱，请查收');}
					else 
					{$data = array('status'=>'0','message'=>'发送邮件时出现错误，请联系客服');}
				}
			}
		}
	}
	
	$ec = json_encode($data);
	echo $ec;
}

if($op=='clockin'){//签到

	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else{
	
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	{
	
	$SQL = "SELECT * FROM `authorize` WHERE `domain`='$user' and `ycp`='$cp'";
	$query=mysqli_query($con,$SQL);
	while($row =mysqli_fetch_array($query))
	{
		$breath = $row[yumi];//用户状态
		$domain = $row[domain];
		$dpass = $row[pass];
		$rmb = $row[rmb];
		$id = $row[id];
		$bz = $row[bz];
		if($breath==1){$breath='0';}else{$breath='1';}//用户状态取反											
	}
	if($user != $domain)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'无授权');}
	else
	{
	
	if($breath == 0)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'禁用中','bz'=>$bz);}
	else
	{	
	$sql="select * from products where cp='$cp'";
	$result=mysqli_query($con,$sql);
	if($result&&mysqli_num_rows($result)){
		while ($row=mysqli_fetch_assoc($result)) {
			$qd = $row['qd'];
			$qd_rmb = $row['qd_rmb'];				
	}}
	if($qd == 0)
	{$data = array('status'=>'0','message'=>'未开放签到');}
	else	
	{
	$time = date("Y/m/d");
	$sql = "select * from log where domain='$id' and cp='$cp' and time='$time'";
	$row=mysqli_fetch_array(mysqli_query($con,$sql));
	if ($row) 
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'今日已签到');}
	else
	{
	
	$zrmb = $rmb + $qd_rmb;
	$sql = "INSERT INTO `log` (`domain`,`time`,`cp`,`rmb`,`zrmb`) VALUES ('$id', '$time', '$cp', '$qd_rmb', '$zrmb');";//添加签到日志
	mysqli_query($con,$sql);
	$dtime = time();
	$qrmb = '+'.$qd_rmb;
	ins_log($user,$dtime,$cp,$qrmb,$zrmb,'每日签到');
	$sql = "UPDATE `authorize` SET `rmb` = '$zrmb' WHERE `id` = '$id';";
	mysqli_query($con,$sql);
	$data = array('status'=>'1','cp'=>$cp,'user'=>$user,'rmb'=>$qd_rmb,'zrmb'=>$zrmb,'time'=>$time);
}}}}}}}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='myquery'){//我的授权
	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else
	{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	{
		
		$numrows=0;

		$result = mysqli_query($con,"SELECT * FROM `authorize` WHERE `qq` LIKE '$user' and `ycp` LIKE '$cp'");
        while($row = mysqli_fetch_array($result)) {
            $content = $content."-".$row['domain'];
			$numrows = $numrows+1;
        }
		
		
		if($numrows != 0)
		{$data = array('status'=>'1','cp'=>$cp,'user'=>$user,'data'=>$content,'num'=>$numrows);}
		else{
			$data = array('status'=>'0','message'=>'一个授权都没有');
		}
	}}}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='query_daili'){//查询代理
	if($qq==null)
	{$qq = array('status'=>'0','message'=>'QQ获取错误');}
	else
	{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else
	{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	{

	$SQL = "SELECT * FROM `daili` WHERE `qq`='$qq' and `ycp`='$cp'";
	$query=mysqli_query($con,$SQL);
	while($row =mysqli_fetch_array($query))
	{
		$uid = $row[id];
		$breath = $row[yumi];//用户状态
		$qq1 = $row[qq];
		$bz = $row[bz];
		$dl = $row['dl'];
		$upuid = $row['up'];
		if($breath==1){$breath='0';}else{$breath='1';}//用户状态取反											
	}
	if($qq != $qq1)
	{$data = array('status'=>'0','cp'=>$cp,'qq'=>$qq,'message'=>'非官方代理');}
	else
	{$data = array('status'=>'1','cp'=>$cp,'qq'=>$qq,'uid'=>$uid,'dl'=>$dl,'upuid'=>$upuid,'bz'=>$bz,'breath'=>$breath);}
	}}}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='query'){//查询授权
	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else
	{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	{

	$SQL = "SELECT * FROM `authorize` WHERE `domain`='$user' and `ycp`='$cp'";
	$query=mysqli_query($con,$SQL);
	while($row =mysqli_fetch_array($query))
	{
		$name = $row[username];
		$breath = $row[yumi];//用户状态
		$domain = $row[domain];
		$dqtime = $row[time];
		$qq = $row[qq];
		$bz = $row[bz];
		$dl = $row['dl'];
		$upuid = $row['up'];
		if($dqtime==null){$timegl = "永久";}//格式化到期时间显示
		else
		if($dqtime=="free"){$timegl = "免费用户";}
		else
		{$timegl = date("Y-m-d H:i:s",$dqtime);}
		if($breath==1){$breath='0';}else{$breath='1';}//用户状态取反											
	}
	if($user != $domain)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'无授权');}
	else
	if($dqtime != null and $dqtime != "free")
	{
		if(time() > $dqtime)
		{$data = array('status'=>'1','state'=>'0','cp'=>$cp,'user'=>$user,'message'=>'已到期','qq'=>$qq,'time'=>$timegl,'upuid'=>$upuid,'bz'=>$bz,'breath'=>$breath);}
		else
		{$data = array('status'=>'1','state'=>'1','cp'=>$cp,'user'=>$user,'name'=>$name,'qq'=>$qq,'dl'=>$dl,'time'=>$timegl,'upuid'=>$upuid,'bz'=>$bz,'breath'=>$breath);}
	}
	else
	{$data = array('status'=>'1','state'=>'1','cp'=>$cp,'user'=>$user,'name'=>$name,'qq'=>$qq,'dl'=>$dl,'time'=>$timegl,'upuid'=>$upuid,'bz'=>$bz,'breath'=>$breath);}
	}}}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='get_alive'){//获取在线人数
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else
	{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	{
	$SQL = "SELECT * FROM `products` WHERE `cp`='$cp'";
	$query=mysqli_query($con,$SQL);
	while($row =mysqli_fetch_array($query))
	{
		$alive = $row['alive'];
	}	
	$nowtime=time();
	$SQL = "SELECT * FROM `authorize` WHERE `ycp`='$cp'";
	$query=mysqli_query($con,$SQL);
	while($row_auth =mysqli_fetch_array($query))
	{
		if($nowtime-$row_auth['lasttime']<$alive){
			$alivenum = $alivenum+1;
		}							
	}
	$data = array('status'=>'1','cp'=>$cp,'alive'=>$alivenum);
	}}
	$ec = json_encode($data);
	echo $ec;
}


if($op=='km_use'){//使用卡密
	if($km==null)
	{$data = array('status'=>'0','message'=>'卡密获取错误');}
	else
	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	{
		$sql="SELECT * FROM `cami` where km='$km' limit 1";//判断卡密
		$row=mysqli_fetch_array(mysqli_query($con,$sql));
		if($row)
		{
			$yme=$row['ytime'];
			$cp=$row['cp'];		
			$kmid=$row['id'];
			$sql="select * from authorize where domain='$user' and ycp='$cp'";//判断用户和产品
			$row=mysqli_fetch_array(mysqli_query($con,$sql));
			if($row)
			{
				$name = $row['username'];
				if($row['time']=="free")
				{
					if($yme == '+100year')
					{$time = '';}
				else
					{$time = strtotime($yme);}
				}
				else
				{
					if($yme == '+100year')
					{$time = '';}
				else
					{
						if(time() > $row['time'])
						{
							$time = strtotime($yme);
						}else
						{$time = strtotime($yme,$row['time']);}
					}
					
				}
				$sql = "UPDATE `authorize` SET `time` = '$time' WHERE domain='$user' and ycp='$cp';";//更新到期时间
				mysqli_query($con,$sql); 
				if($yme == '+100year')
				{$dsc="永久";}
				else
				{$dsc=date("Y-m-d H:i:s",$time);}
				mysqli_query($con,"DELETE FROM `cami` where `id` = $kmid ");//删除卡密
				$status = '已使用';
				$status = base64_encode($status);
				$sql = "INSERT INTO `camilog` (`domain`, `km`, `time`,  `status`,  `ytime`,`cp`) VALUES ('$user', '$km', '$nowtime', '$status', '$yme','$cp');";//添加日志
				mysqli_query($con,$sql);
				$data = array('status'=>'1','mod'=>'1','cp'=>$cp,'user'=>$user,'name'=>$name,'time'=>$dsc);
			}
			else
			{$data = array('status'=>'0','cp'=>$cp,'message'=>'账号不存在1');}
		}
		else
		{
			$sql="SELECT * FROM `cami_ye` where km='$km' limit 1";//判断卡密
			$row=mysqli_fetch_array(mysqli_query($con,$sql));
			if($row)
			{
				$ye=$row['ye'];
				$cp=$row['cp'];		
				$kmid=$row['id'];
				$sql="select * from authorize where domain='$user' and ycp='$cp'";
				$row=mysqli_fetch_array(mysqli_query($con,$sql));
				if($row)
				{
					$name = $row['username'];
					$zye = $row['rmb'] + $ye;
					$sql = "UPDATE `authorize` SET `rmb` = '$zye' WHERE domain='$user' and ycp='$cp';";
					mysqli_query($con,$sql); 
					mysqli_query($con,"DELETE FROM `cami_ye` where `id` = $kmid ");//删除卡密
					$status = '已使用';
					$status = base64_encode($status);
					$sql = "INSERT INTO `camilog` (`domain`, `km`, `time`,  `status`,  `ytime`,`cp`) VALUES ('$user', '$km', '$nowtime', '$status', '$ye','$cp');";
					mysqli_query($con,$sql);
					ins_log($user,$nowtime,$cp,'+'.$ye,$zye,'API使用-余额卡密');
					$data = array('status'=>'1','mod'=>'2','cp'=>$cp,'user'=>$user,'name'=>$name,'rmb'=>$ye,'zrmb'=>$zye);
				}
				else
				{$data = array('status'=>'0','cp'=>$cp,'message'=>'账号不存在');}
			}
			else
			{$data = array('status'=>'0','message'=>'卡密错误或者已被使用');}	
		}	
	}	
	$ec = json_encode($data);
	echo $ec;
}

if($op=='km_use1'){//使用卡密_2
	if($km==null)
	{$data = array('status'=>'0','message'=>'卡密获取错误');}
	else
	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	if($qq==null)
	{$data = array('status'=>'0','message'=>'联系人QQ获取错误');}
	else
	{
		
		$sql="SELECT * FROM `cami` where km='$km' limit 1";//判断卡密
		$row=mysqli_fetch_array(mysqli_query($con,$sql));
		if($row)
		{
			$yme=$row['ytime'];
			$cp=$row['cp'];		
			$kmid=$row['id'];
			$up=$row['up'];
			$sql="select * from authorize where domain='$user' and ycp='$cp'";//判断用户和产品
			$row=mysqli_fetch_array(mysqli_query($con,$sql));
			if($row)
			{
				$name = $row['username'];
				if($row['time']=="free")
				{
					if($yme == '+100year')
					{$time = '';}
				else
					{$time = strtotime($yme);}
				}
				else
				{
					if($yme == '+100year')
					{$time = '';}
				else
					{
						if(time() > $row['time'])
						{
							$time = strtotime($yme);
						}else
						{$time = strtotime($yme,$row['time']);}
					}
					
				}
				$sql = "UPDATE `authorize` SET `time` = '$time' WHERE domain='$user' and ycp='$cp';";//更新到期时间
				mysqli_query($con,$sql); 
				if($yme == '+100year')
				{$dsc="永久";}
				else
				{$dsc=date("Y-m-d H:i:s",$time);}
				mysqli_query($con,"DELETE FROM `cami` where `id` = $kmid ");//删除卡密
				$status = '已使用';
				$status = base64_encode($status);
				$sql = "INSERT INTO `camilog` (`domain`, `km`, `time`,  `status`,  `ytime`,`cp`) VALUES ('$user', '$km', '$nowtime', '$status', '$yme','$cp');";//添加日志
				mysqli_query($con,$sql);
				$data = array('status'=>'1','mod'=>'1','cp'=>$cp,'user'=>$user,'name'=>$name,'time'=>$dsc);
			}
			else
			{
				if($yme == '+100year')
					{$time = '';}
				else
					{$time = strtotime($yme);}
				if($yme == '+100year')
				{$dsc="永久";}
				else
				{$dsc=date("Y-m-d H:i:s",$time);}
				$sql = "INSERT INTO `authorize` (`username`,`domain`,`pass`,`qq`,`time`, `syskey`,`ycp`, `dl`, `up`) VALUES ('$user','$user','$km','$qq','$time','$km','$cp', '普通用户','$up');";//添加授权
				mysqli_query($con,$sql); 
				mysqli_query($con,"DELETE FROM `cami` where `id` = $kmid ");//删除卡密
				$status = '已使用';
				$status = base64_encode($status);
				$sql = "INSERT INTO `camilog` (`domain`, `km`, `time`,  `status`,  `ytime`,`cp`) VALUES ('$user', '$km', '$nowtime', '$status', '$yme','$cp');";//添加日志
				mysqli_query($con,$sql);
				$data = array('status'=>'1','mod'=>'1','cp'=>$cp,'user'=>$user,'name'=>$qq,'time'=>$dsc);
			}
		}
		else
		{
			$sql="SELECT * FROM `cami_ye` where km='$km' limit 1";//判断卡密
			$row=mysqli_fetch_array(mysqli_query($con,$sql));
			if($row)
			{
				$ye=$row['ye'];
				$cp=$row['cp'];		
				$kmid=$row['id'];
				$up=$row['up'];
				$sql="select * from authorize where domain='$user' and ycp='$cp'";
				$row=mysqli_fetch_array(mysqli_query($con,$sql));
				if($row)
				{
					$name = $row['username'];
					$zye = $row['rmb'] + $ye;
					$sql = "UPDATE `authorize` SET `rmb` = '$zye' WHERE domain='$user' and ycp='$cp';";
					mysqli_query($con,$sql); 
					mysqli_query($con,"DELETE FROM `cami_ye` where `id` = $kmid ");//删除卡密
					$status = '已使用';
					$status = base64_encode($status);
					$sql = "INSERT INTO `camilog` (`domain`, `km`, `time`,  `status`,  `ytime`,`cp`) VALUES ('$user', '$km', '$nowtime', '$status', '$ye','$cp');";
					mysqli_query($con,$sql);
					ins_log($user,$nowtime,$cp,'+'.$ye,$zye,'API使用-余额卡密');
					$data = array('status'=>'1','mod'=>'2','cp'=>$cp,'user'=>$user,'name'=>$name,'rmb'=>$ye,'zrmb'=>$zye);
				}
				else
				{
					$sql = "INSERT INTO `authorize` (`username`,`domain`,`pass`,  `qq`,  `time`, `syskey`, `ycp`, `dl`, `rmb`, `up`) VALUES ('$user','$user','$km','$qq',  'free', '$km', '$cp', '普通用户','$ye','$up');";//添加授权
					mysqli_query($con,$sql); 
					mysqli_query($con,"DELETE FROM `cami_ye` where `id` = $kmid ");//删除卡密
					$status = '已使用';
					$status = base64_encode($status);
					$sql = "INSERT INTO `camilog` (`domain`, `km`, `time`,  `status`,  `ytime`,`cp`) VALUES ('$user', '$km', '$nowtime', '$status', '$ye','$cp');";
					mysqli_query($con,$sql);
					ins_log($user,$nowtime,$cp,'+'.$ye,$ye,'API使用-余额卡密');
					$data = array('status'=>'1','mod'=>'2','cp'=>$cp,'user'=>$user,'name'=>$user,'rmb'=>$ye,'zrmb'=>$ye);
				}
			}
			else
			{$data = array('status'=>'0','message'=>'卡密错误或者已被使用');}	
		}	
	}	
	$ec = json_encode($data);
	echo $ec;
}

if($op=='change_user'){//修改账号_key
	if($user_new==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	if($key_user==null)
	{$data = array('status'=>'0','message'=>'用户key获取错误');}
	else
	{
		
		$SQL = "SELECT * FROM `authorize` WHERE `syskey`='$key_user'";
		$query=mysqli_query($con,$SQL);
		$row =mysqli_fetch_array($query);
		$id = $row[id];
		$cp = $row[ycp];
		if(mysqli_num_rows($query) == 1)
		{
			$SQL = "SELECT * FROM `authorize` WHERE `domain`='$user_new' and `ycp`='$cp'";
			$query=mysqli_query($con,$SQL);
			if(mysqli_num_rows($query) == 1)
			{
				$data = array('status'=>'0','message'=>'该账号已存在');
			}
			else
			{
			$data = array('status'=>'1','cp'=>$cp,'user'=>$user_new);
			$sql = "UPDATE `authorize` SET `domain` = '$user_new' WHERE `id` = '$id';";
			mysqli_query($con,$sql);
			}
		}
		else
		{
			$data = array('status'=>'0','message'=>'用户key错误');
		}
	}
	$ec = json_encode($data);
	echo $ec;
}
if($op=='change_user1'){//修改账号_op
	if($newuser==null)
	{$data = array('status'=>'0','message'=>'新账号获取错误');}
	else
	{
	if($user==null)
	{$data = array('status'=>'0','message'=>'旧账号获取错误');}
	else
	{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else
	{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
		
	{
		
		$SQL = "SELECT * FROM `authorize` WHERE `domain`='$user' and `ycp`='$cp'";
		$query=mysqli_query($con,$SQL);
		$row =mysqli_fetch_array($query);
		$id = $row[id];
		if(mysqli_num_rows($query) == 1)
		{
			$SQL = "SELECT * FROM `authorize` WHERE `domain`='$newuser' and `ycp`='$cp'";
			$query=mysqli_query($con,$SQL);
			if(mysqli_num_rows($query) == 1)
			{
				$data = array('status'=>'0','message'=>'该账号已存在');
			}
			else
			{
			$data = array('status'=>'1','cp'=>$cp,'user'=>$newuser);
			$sql = "UPDATE `authorize` SET `domain` = '$newuser' WHERE `id` = '$id';";
			mysqli_query($con,$sql);
			}
		}
		else
		{
			$data = array('status'=>'0','message'=>'账号不存在');
		}
	}}}}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='change_name'){//修改用户名
	if($name_new==null)
	{$data = array('status'=>'0','message'=>'用户名获取错误');}
	else
	if($key_user==null)
	{$data = array('status'=>'0','message'=>'用户key获取错误');}
	else
	{
		$SQL = "SELECT * FROM `authorize` WHERE `syskey`='$key_user'";
		$query=mysqli_query($con,$SQL);
		$row =mysqli_fetch_array($query);
		$id = $row[id];
		$cp = $row[ycp];
		if(mysqli_num_rows($query) == 1)
		{
			$data = array('status'=>'1','state'=>'1','cp'=>$cp,'name'=>$name_new);
			$sql = "UPDATE `authorize` SET `username` = '$name_new' WHERE `id` = '$id';";
			mysqli_query($con,$sql);
		}
		else
		{
			$data = array('status'=>'0','message'=>'用户key错误');
		}
	}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='change_pass'){//修改密码
	if($pass_new==null)
	{$data = array('status'=>'0','message'=>'密码获取错误');}
	else
	if($key_user==null)
	{$data = array('status'=>'0','message'=>'用户key获取错误');}
	else
	{
		$SQL = "SELECT * FROM `authorize` WHERE `syskey`='$key_user'";
		$query=mysqli_query($con,$SQL);
		$row =mysqli_fetch_array($query);
		$id = $row[id];
		$cp = $row[ycp];
		if(mysqli_num_rows($query) == 1)
		{
			$data = array('status'=>'1','state'=>'1','cp'=>$cp,'pass'=>$pass_new);
			$sql = "UPDATE `authorize` SET `pass` = '$pass_new' WHERE `id` = '$id';";
			mysqli_query($con,$sql);
		}
		else
		{
			$data = array('status'=>'0','message'=>'用户key错误');
		}
	}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='get_key'){//获取用户key
	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	if($pass==null)
	{$data = array('status'=>'0','message'=>'密码获取错误');}
	else
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else
	{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	{
		$SQL = "SELECT * FROM `authorize` WHERE `domain`='$user'  and `ycp`='$cp'";
		$query=mysqli_query($con,$SQL);
		$row =mysqli_fetch_array($query);
		$key = $row[syskey];
		$dpass = $row[pass];
		if(mysqli_num_rows($query) == 1)
		{
			if($pass != $dpass)
			{$data = array('status'=>'0','message'=>'密码错误');}
		else{
		$data = array('status'=>'1','state'=>'1','cp'=>$cp,'user'=>$user,'key_user'=>$key);}
		}
		else
		{
			$data = array('status'=>'0','message'=>'账号不存在');
		}
	}}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='detect'){//检测更新
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else
	{
	$SQL = "SELECT * FROM `products` WHERE `nkey`='$key_cp'";
	$query=mysqli_query($con,$SQL);
	$row =mysqli_fetch_array($query);
	$notice = $row[ms];
	$cp = $row[cp];
	$newver = $row[nver];
	$qz = $row[qz];
	$url = $row[url];
	$zt = $row[zt];
	$qt = $row[qt];
	$kf = $row[kf];
	$qd = $row[qd];
	$qd_rmb = $row[qd_rmb];
	$time = date("Y-m-d H:i:s",$row[time]);
	$free = $row[free];
	if(mysqli_num_rows($query) == 1)//是最新版
	{
		$newtxt = get_txt($key_cp);
		$data = array('status'=>'1','state'=>'1','cp'=>$cp,'zt'=>$zt,'qt'=>$qt,'kf'=>$kf,'qd'=>$qd,'qd_rmb'=>$qd_rmb,'free'=>$free,'notice'=>$notice,'newver'=>$newver,'qz'=>$qz,'url'=>$url,'time'=>$time,'newtxt'=>$newtxt);}
	else{
			$SQL = "SELECT * FROM `version` WHERE `dkey`='$key_cp'";
			$query=mysqli_query($con,$SQL);
			$row =mysqli_fetch_array($query);
			if(mysqli_num_rows($query) == 1)
			{
				$cp = $row[cp];
				$nowver = $row[ver];
				$SQL = "SELECT * FROM `products` WHERE `cp`='$cp'";
				$query=mysqli_query($con,$SQL);
				$row =mysqli_fetch_array($query);
				$free = $row[free];
				$notice = $row[ms];
				$newver = $row[nver];
				$qz = $row[qz];
				$qt = $row[qt];
				$kf = $row[kf];
				$qd = $row[qd];
				$qd_rmb = $row[qd_rmb];
				$url = $row[url];
				$zt = $row[zt];
				$nkey = $row[nkey];
				$time = date("Y-m-d H:i:s",$row[time]);
				$newtxt = get_txt($nkey);
				$data = array('status'=>'1','state'=>'0','cp'=>$cp,'zt'=>$zt,'qt'=>$qt,'kf'=>$kf,'qd'=>$qd,'qd_rmb'=>$qd_rmb,'free'=>$free,'notice'=>$notice,'newver'=>$newver,'nowver'=>$nowver,'qz'=>$qz,'url'=>$url,'time'=>$time,'newtxt'=>$newtxt);
			}
			else
			{$data = array('status'=>'0','message'=>'产品key错误');}
	}}
		$ec = json_encode($data);
		echo $ec;
}


if($op=='login'){//登录
	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	{
	if($pass==null)
	{$data = array('status'=>'0','message'=>'密码获取错误');}
	else	
	{
	$cp = get_cp($key_cp);
	$ver = get_ver($key_cp);
	$isfree = get_isfree($cp);
	$SQL = "SELECT * FROM `authorize` WHERE `domain`='$user' and `ycp`='$cp'";
	$query=mysqli_query($con,$SQL);
	while($row =mysqli_fetch_array($query))
	{
		$name = $row[username];
		$breath = $row[yumi];//用户状态
		$domain = $row[domain];
		$dqtime = $row[time];
		$dpass = $row[pass];
		$qq = $row[qq];
		$rmb = $row[rmb];
		$id = $row[id];
		$key = $row[syskey];
		$bz = $row[bz];
		$dl = $row['dl'];
		$lasttime = time();
		if($dqtime==null){$timegl = "永久";}//格式化到期时间显示
		else
		if($dqtime=="free"){$timegl = "免费用户";}
		else
		{$timegl = date("Y-m-d H:i:s",$dqtime);}
		if($breath==1){$breath='0';}else{$breath='1';}//用户状态取反											
	}
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	if($user != $domain)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'无授权或者账号不存在');}
	else
	if($dpass != $pass)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'密码错误');}
	else
	if($breath == 0)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'禁用中','bz'=>$bz);}
	else	
	if($dqtime == "free" and $isfree == 0)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'非免费产品','bz'=>'您当前是免费用户，无法使用收费产品');}
	else	
	if($dqtime != null and $dqtime != "free")
	{
		if(time() > $dqtime)
		{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'已到期','time'=>$timegl);}
		else
		{
			$lastip=getip();
			$data = array('status'=>'1','cp'=>$cp,'user'=>$user,'name'=>$name,'qq'=>$qq,'dl'=>$dl,'rmb'=>$rmb,'key'=>$key,'time'=>$timegl,'breath'=>$breath);
			$sql = "UPDATE `authorize` SET `version` = '$ver', `lasttime` = '$lasttime', `lastip` = '$lastip' WHERE `id` = '$id';";
			mysqli_query($con,$sql);
		}
	}
	else
	{
		$lastip=getip();
		$data = array('status'=>'1','cp'=>$cp,'user'=>$user,'name'=>$name,'qq'=>$qq,'dl'=>$dl,'rmb'=>$rmb,'key'=>$key,'time'=>$timegl,'breath'=>$breath);
		$sql = "UPDATE `authorize` SET `version` = '$ver', `lasttime` = '$lasttime', `lastip` = '$lastip' WHERE `id` = '$id';";
		mysqli_query($con,$sql);
	}}}
	
	$ec = json_encode($data);
	echo $ec;
}

if($op=='login_qq'){//qq登录
	
	if($qq==null)
	{$data = array('status'=>'0','message'=>'qq获取错误');}
	else	
	{
	$cp = get_cp($key_cp);
	$ver = get_ver($key_cp);
	$isfree = get_isfree($cp);
	$SQL = "SELECT * FROM `authorize` WHERE `qq`='$qq' and `ycp`='$cp'";
	$query=mysqli_query($con,$SQL);
	while($row =mysqli_fetch_array($query))
	{
		$name = $row[username];
		$breath = $row[yumi];//用户状态
		$domain = $row[domain];
		$dqtime = $row[time];
		$dpass = $row[pass];
		$qq1 = $row[qq];
		$rmb = $row[rmb];
		$id = $row[id];
		$bz = $row[bz];
		$dl = $row['dl'];
		$lasttime = time();
		if($dqtime==null){$timegl = "永久";}//格式化到期时间显示
		else
		if($dqtime=="free"){$timegl = "免费用户";}
		else
		{$timegl = date("Y-m-d H:i:s",$dqtime);}
		if($breath==1){$breath='0';}else{$breath='1';}//用户状态取反											
	}
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	if($qq != $qq1)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$domain,'message'=>'无授权或者账号不存在');}
	else
	if($breath == 0)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$domain,'message'=>'禁用中','bz'=>$bz);}
	else
	if($dqtime == "free" and $isfree == 0)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'非免费产品','bz'=>'您当前是免费用户，无法使用收费产品');}
	else			
	if($dqtime != null and $dqtime != "free")
	{
		if(time() > $dqtime)
		{$data = array('status'=>'0','cp'=>$cp,'user'=>$domain,'message'=>'已到期','time'=>$timegl);}
		else
		{
			$lastip=getip();
			$data = array('status'=>'1','cp'=>$cp,'user'=>$domain,'name'=>$name,'qq'=>$qq,'dl'=>$dl,'rmb'=>$rmb,'time'=>$timegl,'breath'=>$breath);
			$sql = "UPDATE `authorize` SET `version` = '$ver', `lasttime` = '$lasttime', `lastip` = '$lastip' WHERE `id` = '$id';";
			mysqli_query($con,$sql);
		}
	}
	else
	{
		$lastip=getip();
		$data = array('status'=>'1','cp'=>$cp,'user'=>$domain,'name'=>$name,'qq'=>$qq,'dl'=>$dl,'rmb'=>$rmb,'time'=>$timegl,'breath'=>$breath);
		$sql = "UPDATE `authorize` SET `version` = '$ver', `lasttime` = '$lasttime', `lastip` = '$lastip' WHERE `id` = '$id';";
		mysqli_query($con,$sql);
	}}

	$ec = json_encode($data);
	echo $ec;
}

if($op=='login_qq_user'){//qq登录_user
	if($qq==null)
	{$data = array('status'=>'0','message'=>'qq获取错误');}
	else	
	{
	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else	
	{
	$cp = get_cp($key_cp);
	$ver = get_ver($key_cp);
	$isfree = get_isfree($cp);
	$SQL = "SELECT * FROM `authorize` WHERE `qq`='$qq' and `ycp`='$cp'  and `domain`='$user'";
	$query=mysqli_query($con,$SQL);
	while($row =mysqli_fetch_array($query))
	{
		$name = $row[username];
		$breath = $row[yumi];//用户状态
		$domain = $row[domain];
		$dqtime = $row[time];
		$dpass = $row[pass];
		$qq1 = $row[qq];
		$rmb = $row[rmb];
		$id = $row[id];
		$bz = $row[bz];
		$dl = $row['dl'];
		$lasttime = time();
		if($dqtime==null){$timegl = "永久";}//格式化到期时间显示
		else
		if($dqtime=="free"){$timegl = "免费用户";}
		else
		{$timegl = date("Y-m-d H:i:s",$dqtime);}
		if($breath==1){$breath='0';}else{$breath='1';}//用户状态取反											
	}
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	if($qq != $qq1)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$domain,'message'=>'无授权或者账号不存在');}
	else
	if($breath == 0)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$domain,'message'=>'禁用中','bz'=>$bz);}
	else
	if($dqtime == "free" and $isfree == 0)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'非免费产品','bz'=>'您当前是免费用户，无法使用收费产品');}
	else			
	if($dqtime != null and $dqtime != "free")
	{
		if(time() > $dqtime)
		{$data = array('status'=>'0','cp'=>$cp,'user'=>$domain,'message'=>'已到期','time'=>$timegl);}
		else
		{
			$lastip=getip();
			$data = array('status'=>'1','cp'=>$cp,'user'=>$domain,'name'=>$name,'qq'=>$qq,'dl'=>$dl,'rmb'=>$rmb,'time'=>$timegl,'breath'=>$breath);
			$sql = "UPDATE `authorize` SET `version` = '$ver', `lasttime` = '$lasttime', `lastip` = '$lastip' WHERE `id` = '$id';";
			mysqli_query($con,$sql);
		}
	}
	else
	{
		$lastip=getip();
		$data = array('status'=>'1','cp'=>$cp,'user'=>$domain,'name'=>$name,'qq'=>$qq,'dl'=>$dl,'rmb'=>$rmb,'time'=>$timegl,'breath'=>$breath);
		$sql = "UPDATE `authorize` SET `version` = '$ver', `lasttime` = '$lasttime', `lastip` = '$lastip' WHERE `id` = '$id';";
		mysqli_query($con,$sql);
	}}}

	$ec = json_encode($data);
	echo $ec;
}

if($op=='ins_dl'){//添加代理

	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	{
	if($qq==null)
	{$data = array('status'=>'0','message'=>'QQ获取错误');}
	else
	{
	if($pass==null)
	{$data = array('status'=>'0','message'=>'密码获取错误');}
	else
	{
	if($dl==null)
	{$data = array('status'=>'0','message'=>'级别获取错误');}
	else
	{	
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	{
	$sql="select * from daili where domain='$user' and ycp='$cp'";
	$query=mysqli_query($con,$sql);
	$rows = mysqli_num_rows($query);
	if($rows > 0){
		$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'该代理已存在');
		}
	else
	{
	$time = time();
	$sql = "INSERT INTO `daili` (`domain`,`pass`,`qq`,`time`,`yumi`,`ycp`,`rmb`,`dl`,`bz`,`num`) VALUES ('$user','$pass','$qq','$time','0','$cp','0','$dl','',0);";
	$data = array('status'=>'1','cp'=>$cp,'user'=>$user,'qq'=>$qq,'dl'=>$dl);
	mysqli_query($con,$sql);
	}}}}}}}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='del_dl'){//删除代理
	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else{
		
		$sql="select * from daili where domain='$user' and ycp='$cp'";
		$query=mysqli_query($con,$sql);
		$row = mysqli_fetch_array($query);
		$rows = mysqli_num_rows($query);
		$id = $row[id];
		if($rows == 0)
		{$data = array('status'=>'0','message'=>'代理不存在');}
		else
		{
			$sql="DELETE FROM `daili` WHERE `id`='$id'";
			mysqli_query($con,$sql);
			$data = array('status'=>'1','message'=>'删除成功');
		}		
	}}}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='make_dl'){//操作代理

	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else
	{
	if($mod==null)
	{$data = array('status'=>'0','message'=>'指令获取错误');}
	else
	{
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else
	{
	$SQL = "SELECT * FROM `daili` WHERE `domain`='$user' and `ycp`='$cp'";
	$query=mysqli_query($con,$SQL);
	while($row =mysqli_fetch_array($query))
	{
		$breath = $row[yumi];//用户状态
		$domain = $row[domain];
		$rmb = $row[rmb];
		$id = $row[id];
		if($breath==1){$breath='0';}else{$breath='1';}//用户状态取反											
	}
	if($user != $domain)
	{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'非代理');}
	else
	{
	
		if($mod == 1)
		{
			if($mod==1){$mod=0;}else{$mod=1;}	
			if($breath == 1)
			{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'已经是正常状态');}
			else
			{	
			$sql = "UPDATE `daili` SET `yumi` = '$mod' WHERE `id` = '$id';";
			mysqli_query($con,$sql);
			$data = array('status'=>'1','cp'=>$cp,'user'=>$user,'breath'=>'1');
			}
		}
		else
		if($mod == 0)
		{
			if($mod==1){$mod=0;}else{$mod=1;}	
			if($breath == 0)
			{$data = array('status'=>'0','cp'=>$cp,'user'=>$user,'message'=>'已经是禁用状态');}
			else
			{	
			$sql = "UPDATE `daili` SET `yumi` = '$mod' WHERE `id` = '$id';";
			mysqli_query($con,$sql);
			$data = array('status'=>'1','cp'=>$cp,'user'=>$user,'breath'=>'0');
			}
		}
		
	}
	
	}}}}
	$ec = json_encode($data);
	echo $ec;
}

if($op=='site_dl'){//设置代理级别
	if($user==null)
	{$data = array('status'=>'0','message'=>'账号获取错误');}
	else{
	if($dl==null)
	{$data = array('status'=>'0','message'=>'级别获取错误');}
	else{	
	if($key_cp==null)
	{$data = array('status'=>'0','message'=>'产品key获取错误');}
	else{
	$cp = get_cp($key_cp);
	if($cp=="error")
	{$data = array('status'=>'0','message'=>'产品key错误');}
	else{
		
		$sql="select * from daili where domain='$user' and ycp='$cp'";
		$query=mysqli_query($con,$sql);
		$row = mysqli_fetch_array($query);
		$rows = mysqli_num_rows($query);
		$id = $row[id];
		if($rows == 0)
		{$data = array('status'=>'0','message'=>'代理不存在');}
		else
		{
			$sql = "UPDATE `daili` SET `dl` = '$dl' WHERE `id` = '$id';";
			mysqli_query($con,$sql);
			$data = array('status'=>'1','message'=>'设置代理级别成功');
		}		
	}}}}
	$ec = json_encode($data);
	echo $ec;
}



















mysqli_close($con);
//函数定义

function ins_log($user,$time,$cp,$rmb,$zrmb,$juti){//用传入的产品名去获取是否免费产品
	global $con;
	$ip = getip();
	$sql = "INSERT INTO `log_rmb` (`domain`,`time`,`cp`,`rmb`,`zrmb`,`bz`,`ip`) VALUES ('$user', '$time', '$cp', '$rmb', '$zrmb', '$juti','$ip');";//添加流水日志
	mysqli_query($con,$sql);
}
function cp_pd($cp){//传入产品名，判断产品是否存在
	global $con;
	$SQL = "SELECT * FROM `products` WHERE `cp`='$cp'";
	$query=mysqli_query($con,$SQL);
	if( mysqli_num_rows($query) == 0)
	{return 0;}else{return 1;}
}
function get_txt($key_cp){//用传入的产品key去获取最新版本的更新内容
	global $con;
	$SQL = "SELECT * FROM `version` WHERE `dkey`='$key_cp'";
	$query=mysqli_query($con,$SQL);
	$row =mysqli_fetch_array($query);
	return $row[ddata];
}
function get_cp($key_cp){//用传入的产品key去获取对应的产品名
	global $con,$free;
	$SQL = "SELECT * FROM `products` WHERE `nkey`='$key_cp'";
	$query=mysqli_query($con,$SQL);
	$row =mysqli_fetch_array($query);
	$cp = $row[cp];
	$free = $row[free];
	if($free !=1){$free==0;}
	if(mysqli_num_rows($query) == 1)
	{return $cp;}
	else
	{
		$SQL = "SELECT * FROM `version` WHERE `dkey`='$key_cp'";
		$query=mysqli_query($con,$SQL);
		$row =mysqli_fetch_array($query);
		if(mysqli_num_rows($query) == 1)
		{
			$cp = $row[cp];
			return $cp;
		}
		else
		{return "error";}
	}
}
function get_ver($key_cp){//用传入的产品key去获取对应的版本
	global $con;
	$SQL = "SELECT * FROM `products` WHERE `nkey`='$key_cp'";
	$query=mysqli_query($con,$SQL);
	$row =mysqli_fetch_array($query);
	$ver = $row[nver];
	if(mysqli_num_rows($query) == 1)
	{return $ver;}
	else
	{
		$SQL = "SELECT * FROM `version` WHERE `dkey`='$key_cp'";
		$query=mysqli_query($con,$SQL);
		$row =mysqli_fetch_array($query);
		if(mysqli_num_rows($query) == 1)
		{
			$ver = $row[ver];
			return $ver;
		}
		else
		{return "error";}
	}
}
function get_isfree($cp){//用传入的产品名去获取是否免费产品
	global $con;
	$SQL = "SELECT * FROM `products` WHERE `cp`='$cp'";
	$query=mysqli_query($con,$SQL);
	$row =mysqli_fetch_array($query);
	$isfree = $row[free];
	if(mysqli_num_rows($query) == 1)
	{return $isfree;}
	else
	{return "error";}
}
function validation($key_sys){//验证提交的key_sys是否正确-10秒窗口期
	global $con,$title,$yxtime,$copyright,$site_name,$route,$robotqq,$apikey;//声明使用全局变量
	get_sys();
	$time=substr(time(),0,-1);
	//$time=time();
	$jm_key = md5($apikey);
	$jm = $jm_key.$time;
	$jm =  md5($jm);
	if($key_sys == $jm)
	{return 1;}
	else
	{return 0;}
}
function validation1($key_sys){//验证提交的key_sys是否正确，发邮件专用-1000秒窗口期
	global $con,$title,$yxtime,$copyright,$site_name,$route,$robotqq,$apikey;//声明使用全局变量
	get_sys();
	$time=substr(time(),0,-3);
	//$time=time();
	$jm_key = md5($apikey);
	$jm = $jm_key.$time;
	$jm =  md5($jm);
	if($key_sys == $jm)
	{return 1;}
	else
	{return 0;}
}
function get_sys(){//获取系统变量，并赋值给全局变量
	global $con,$title,$yxtime,$copyright,$site_name,$route,$robotqq,$apikey,$smtpserver,$smtpserverport,$smtpusermail,$smtpuser,$smtppass,$kmqz,$kmcd;//声明使用全局变量
	$sql = "SELECT * FROM site"; 
	$result=mysqli_query($con,$sql);
	$row=mysqli_fetch_assoc($result); 
	$title = $row['title'];
	$yxtime = $row['yxtime'];
	$copyright = $row['copyright'];
	$site_name = $row['site_name'];
	$route = $row['route'];
	$robotqq = $row['robotqq'];
	$apikey = $row['apikey'];
	$smtpserver = $row['smtpserver'];
	$smtpserverport = $row['smtpserverport'];
	$smtpusermail = $row['smtpusermail'];
	$smtpuser = $row['smtpuser'];
	$smtppass = $row['smtppass'];

	$sql = "SELECT * FROM selfhelp"; 
	$result=mysqli_query($con,$sql);
	$row=mysqli_fetch_assoc($result); 
	$kmqz = $row['kmqz'];
	$kmcd = $row['kmcd'];
}
//生成卡密
function getkm()
{
	global $con,$kmqz,$kmcd;//声明使用全局变量
	$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	$strlen = strlen($str);
	$randstr = "";
	for ($i = 0; $i < $kmcd; $i++) {
		$randstr .= $str[mt_rand(0, $strlen - 1)];
	}
	return $kmqz.$randstr;
}
// function getip(){//获取用户IP地址
// error_reporting (E_ERROR | E_WARNING | E_PARSE);
// if($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]){
	// $ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
// }
// elseif($HTTP_SERVER_VARS["HTTP_CLIENT_IP"]){
	// $ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
// }
// elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"]){
	// $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
// }
// elseif (getenv("HTTP_X_FORWARDED_FOR")){
	// $ip = getenv("HTTP_X_FORWARDED_FOR");
// }
// elseif (getenv("HTTP_CLIENT_IP")){
	// $ip = getenv("HTTP_CLIENT_IP");
// }
// elseif (getenv("REMOTE_ADDR")){
	// $ip = getenv("REMOTE_ADDR");
// }
// else{
	// $ip = "Unknown";
// }
// return $ip;
// }
function getIp(){
    $ip='未知IP';
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
        return is_ip($_SERVER['HTTP_CLIENT_IP'])?$_SERVER['HTTP_CLIENT_IP']:$ip;
    }elseif(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        return is_ip($_SERVER['HTTP_X_FORWARDED_FOR'])?$_SERVER['HTTP_X_FORWARDED_FOR']:$ip;
    }else{
        return is_ip($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:$ip;
    }
}
function is_ip($str){
    $ip=explode('.',$str);
    for($i=0;$i<count($ip);$i++){ 
        if($ip[$i]>255){ 
            return false; 
        } 
    } 
    return preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/',$str); 
}

function json_products($status,$message="",$data=array()){ //定义一个json格式
            $result=array(  
              'status'=>$status,  
              'message'=>$message,  
              'products'=>$data   
            );  
            return json_encode($result);  
}
//mysqli_close();
?>